//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    news:[],
    height:400,
    pageIndex:1,
  },
  loading:false,
  //事件处理函数
  bindViewTap: function() {
    wx.showToast({"title":"测试一下"});
  },
  //
  copyrightTap: function(){
    wx.showToast({"title":"cc"});
  },
  _loadData: function(pageIndex){
    var self = this;
    wx.showLoading({"title":"获取数据中"});
    wx.request({
      url:'https://yys.163.com/news/index'+(this.data.pageIndex==1?"":("_"+this.data.pageIndex))+".html",
      dataType:'text',
      success:function(res)
      {
        var newsArray = [];
        var newsDt = res.data.split("news-list")[1].split("pager")[0];
        var index = 0;
        var end = 0;
        
        while(index!=-1&&end!=-1)
        {
          let ct = false;
          var news = {};
          /// 查找超链接
          index = end+7;
          index = newsDt.indexOf("href=\"",index);
          end = newsDt.indexOf("\"",index+6);
          var link = newsDt.substring(index+6,end);
          if(link.length==0||index==-1)break;
          if(/http/.test(link))
          {
            ct = true;
          }
          news.link = "https:"+link;
          /// 查找标题
          index = newsDt.indexOf("p-tit",end+1);
          index = newsDt.indexOf(">",index);
          end = newsDt.indexOf("</p>",index+1);
          news.title = newsDt.substring(index+1,end);
          /// 查找摘要
          index = newsDt.indexOf("p-mess",end+4);
          index = newsDt.indexOf(">",index);
          
          end = newsDt.indexOf("</p>",index+1);
          news.desc = newsDt.substring(index+1,end);
          /// 没有描述~
          if(news.desc.length==0)
          {
            news.desc = "网易2016自研精品，唯美空灵的和风写意，经典的半即时回合制RPG，卡牌收集、养成，基于LBS地理技术的社交PK，上百种式神亟待觉醒";
          }
          /// 查找分类
          index = newsDt.indexOf("category",end+4);
          index = newsDt.indexOf(">",index);
          end = newsDt.indexOf("</span>",index+1);
          news.category = newsDt.substring(index+1,end);
          /// 推进数组中
          if(!ct)
            newsArray.push(news);
        }
        /// 设置数据源
        self.setData({news:self.data.news.concat(newsArray)});
      },
      fail:function()
      {
        wx.showToast({"title":"获取数据失败"});
        self.data.pageIndex--;
      },
      complete:function(){
        self.loading = false;
        wx.hideLoading()
      }
    });
  },
  loadMore:function(){
    if(this.loading)return;
    this.loading = true;
    this.data.pageIndex++;
    this._loadData();
  },
  itemTaped:function(e){
    /// 利用同步存储连接
    var dt = this.data.news[e.currentTarget.dataset.index];
    wx.setStorageSync("tmplink",dt.link);
    wx.setStorageSync("tmptitle",dt.title);
    /// 新闻详细
    wx.navigateTo({url:"/pages/news/newsDetail/newsDetail"});
  },
  onLoad: function () {
    var that = this
    wx.getSystemInfo({success:function(res){
      that.setData({height:res.windowHeight});
    }});
    this._loadData();
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  }
})
